package com.cloud.zuul.filer;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.ROUTE_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVLET_DETECTION_FILTER_ORDER;

import java.io.IOException;

import org.springframework.stereotype.Component;

import com.cloud.zuul.exception.RateLimitExcption;
import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

@Component
public class AccessFilter extends ZuulFilter {
	
	private static final RateLimiter RATE_LIMITER=RateLimiter.create(100);

	@Override
	public Object run() throws ZuulException {
		if(!RATE_LIMITER.tryAcquire()) {//限流
			throw new RateLimitExcption();
		}
		RequestContext ctx = RequestContext.getCurrentContext();
//				ctx.getResponse().sendRedirect("/user/login");
//			ctx.setSendZuulResponse(false);
////		return ctx;
		return null;
	}

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public int filterOrder() {
		return SERVLET_DETECTION_FILTER_ORDER -1;
	}

	@Override
	public String filterType() {
//		return PRE_TYPE;
		return ROUTE_TYPE;
	}

}
